
#include "../utils//logger.h"
#include "../utils/FileUtil.h"
#include "../utils/StringUtil.h"
#include "Test.h"
#include "DataKitIO.h"

#ifndef CXX_LESS_17
void TestDataKit(const std::filesystem::path dataFolder)
#else
void TestDataKit(const std::string  dataFolder)
#endif
{
    #ifndef CXX_LESS_17
    std::filesystem::path inputFolder = dataFolder;
    #else
        std::string  inputFolder = dataFolder;
    #endif
    
    #ifndef CXX_LESS_17
    if (!IsDirExist(inputFolder.string()))
    #else
    if (!IsDirExist(inputFolder))
    #endif
    {
        DEBUG("folder " << inputFolder << " not exist");
        return;
    }

    #ifndef CXX_LESS_17
    std::filesystem::path outFoler = dataFolder / ("debug");
    
    if (std::filesystem::exists(outFoler))
    {
        std::filesystem::remove_all(outFoler);
    }
    std::filesystem::create_directories(outFoler);
    #else
    std::string outFoler = dataFolder +"/" + ("debug");
    
    if (IsDirExist(outFoler)) {
        RemoveDir(outFoler) ;
    }
    MakeDir(outFoler) ;
    #endif
    
    std::vector<std::string> fileList;
    TraverseDirectory(inputFolder, &fileList);
    if (fileList.size() <= 0)
    {
        DEBUG("folder " << inputFolder << " is empty");
        return;
    }
    
    auto err = AMCAX::Exchange::DataKit::IO::GetInstance().Initialize();
    if (err != 0)
    {
        DEBUG("AMCAX::Exchange::DataKit::IO Initialize with error: "  << err);
        return;
    }

    int currIndex = 0;
    for (const auto &filePath : fileList)
    {
        auto inputExtension = StringUtil::ToLowerCase(GetExtension(filePath));
        // if(inputExtension != ".stp")
        //     continue;
        ++currIndex;
        int fileIndex = 1; // This is the index file which we wants to test.
        if(currIndex != fileIndex)
            continue;
        std::string realFileName = GetFilenameWithoutExtension(filePath);

        //if(realFileName!="06torus") continue;
        bool isDebug = true ;
        // std::string fileType = isDebug?"brep":"catpart" ;
        std::string fileType = "obj";
        // std::filesystem::path currentOutputFolder = outFoler / fileType;
        // if (!std::filesystem::exists(currentOutputFolder))
        // {
        //     std::filesystem::create_directories(currentOutputFolder);
        // }
        // std::filesystem::path outFilePath = currentOutputFolder / (realFileName + "." + fileType);
        // DEBUG("ready to convert file:" << filePath) ;
        // Convert(filePath, outFilePath.string(),isDebug);
        #ifndef CXX_LESS_17
        std::filesystem::path currentOutputFolder = outFoler / fileType;
        if (!std::filesystem::exists(currentOutputFolder))
        {
            std::filesystem::create_directories(currentOutputFolder);
        }
        std::filesystem::path outFilePath = currentOutputFolder / (realFileName + "." + fileType);
        DEBUG("ready to convert file:" << filePath);
        Convert(filePath, outFilePath.string(), isDebug);
        #else
        std::string currentOutputFolder = outFoler + "/" + fileType ;
        if(!IsDirExist(currentOutputFolder))
        {
            MakeDir(currentOutputFolder) ;
        }
        std::string outFilePath = currentOutputFolder + "/" + realFileName + "." + fileType ;
        Convert(filePath, outFilePath, isDebug);
        #endif
        break; // NOTE：只测试第一个文件
    }

    //err = AMCAX::Exchange::DataKit::IO::GetInstance().Terminate();
    if (err != 0)
    {
        DEBUG("AMCAX::Exchange::DataKit::IO Terminate with error: "  << err);
        return;
    }    
}
